<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Math.random</title>
</head>
<body>
    <script>
        // [0,1)
        console.log(Math.random());

        //[0,5)
        console.log(Math.floor(Math.random() * 5));

        //[0,5]
        //Math.floor(Math.random() * (max + 1))
        console.log(Math.floor(Math.random() * (5 + 1)));

        //[2,5]
        // min + Math.floor(Math.random() * (max - min + 1))
        console.log(2 + Math.floor(Math.random() * (5 - 2 + 1)));


        // 随机抽名字
        let student = ['张三','李四','王五','赵六','孙七'];
        let index = Math.floor(Math.random() * student.length);
        console.log(student[index]);


        function arrayRandomValue(array,start = 1,end) {
            end = end ? end : array.length;
            start--;
            const idx = start + Math.floor(Math.random() * (end - start));
            return array[idx];
        }

        console.log(arrayRandomValue(student,4,5));
        console.log(arrayRandomValue([1,2536,6437,7,7657,85448,48],4,5));

    </script>
</body>
</html>